﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reborn_Server.App1AppHandler;
using System.Net.Sockets;
using Reborn_WorldServer;

namespace App1AppHandler
{
    public class Vendor : Creature
    {

        public int PriceAdjustPercentLow = 100;
        public int PriceAdjustPercentHigh = 100;
        
        public List<Loot> VendorLootItems = new List<Loot>();
        public volatile List<Loot> PawnItems = new List<Loot>();

        List<VendorPriceOverride> VendorSpecialPrices = new List<VendorPriceOverride>();

        public Vendor(AppHandler2 gameHander) : base(gameHander) 
        {
            //GroupNumber = 0;
        }

        public override int HP
        {
            set
            {
                base.HP = base.MaxHP;
            }
        }

        public override bool grantExpTo(Character character)
        {
            return false;
        }

        private Loot translateItemPrice(Loot rawItem)
        {
            Loot translatedLoot = (Loot)rawItem.Clone();

            translatedLoot.PriceLow = System.Convert.ToInt32(translatedLoot.PriceLow * (this.PriceAdjustPercentLow / 100.00));
            translatedLoot.PriceHigh = System.Convert.ToInt32(translatedLoot.PriceHigh * (this.PriceAdjustPercentHigh / 100.00));

            return translatedLoot;
        }
        private List<Loot> translateItemPrices(List<Loot> rawItems)
        {
            List<Loot> translatedPrices = new List<Loot>();

            foreach (Loot loot in rawItems)
                translatedPrices.Add(translateItemPrice(loot));

            return translatedPrices;
        }

        public void OpenVendorWindow(Character character)
        {
            if (this.MapID != character.MapID ||
                Math.Abs(this.MapX - character.MapX) > 2 || Math.Abs(this.MapY - character.MapY) > 2)
            {
                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Vendor is to far away";

                character.PlaySound("Ding", character.MapX, character.MapY);
                character.AddMessage(message);
                return;
            }

            VendorItems vendorItems = new VendorItems();

            vendorItems.VendorCreatureID = CreatureID;
            vendorItems.VendorName = Name;

            if (character.LeftHand != null)
                vendorItems.BuyItems.Add(character.LeftHand);
            if (character.RightHand != null)
                vendorItems.BuyItems.Add(character.RightHand);

            List<Loot> characterBag = character.bagManager.getAllBagItems();
            foreach (Loot loot in characterBag)
                vendorItems.BuyItems.Add(loot);

            Loot[] characterBelt = character.beltManager.getBeltLootCopy();
            foreach (Loot loot in characterBelt)
            {
                if (loot != null)
                    vendorItems.BuyItems.Add(loot);
            }

            foreach (Loot loot in VendorLootItems)
                vendorItems.VendorSellItems.Add(loot);

            vendorItems.BuyItems = translateItemPrices(vendorItems.BuyItems);
            vendorItems.VendorSellItems = translateItemPrices(vendorItems.VendorSellItems);

            try
            {
                character.ClientSocket.socket.BeginSend(vendorItems.toXML(character));
            }
            catch { }
        }
        public void BuyVendorItem(Character character, long ItemGUID)
        {
            if (this.MapID != character.MapID ||
                Math.Abs(this.MapX - character.MapX) > 2 || Math.Abs(this.MapY - character.MapY) > 2)
            {
                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Vendor is to far away";

                character.PlaySound("Ding", character.MapX, character.MapY);
                character.AddMessage(message);
                sendErrorNotification(character);
                return;
            }

            Loot ItemToBuy = null;
            foreach (Loot loot in VendorLootItems)
            {
                if (loot.GameUID == ItemGUID)
                {
                    ItemToBuy = translateItemPrice(loot);
                    break;
                }
            }

            if (ItemToBuy == null)
            {
                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Cannot find item to purchase";

                character.PlaySound("Ding", character.MapX, character.MapY);
                character.AddMessage(message);
                sendErrorNotification(character);
                return;
            }

            if (ItemToBuy.Lore == true && character.hasItem(ItemToBuy.ID))
            {
                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "You cannot carry any more of those!";

                character.PlaySound("Ding", character.MapX, character.MapY);
                character.AddMessage(message);
                sendErrorNotification(character);
                return;
            }

            if (character.Coins < ItemToBuy.PriceHigh)
            {
                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Not enough coins to purchase " + ItemToBuy.Name;

                character.PlaySound("Ding", character.MapX, character.MapY);
                character.AddMessage(message);
                sendErrorNotification(character);
                return;
            }

            if (ItemToBuy.Bag == true)
            {
                //Check if we have a slot before we run CreateItem
                if (character.bagManager.SlotsAvailable() > 0)
                {
                    //Attempt to add the new item to the bag.
                    if (character.bagManager.addBagItem(Managers.GameHandler.CreateItem(character, ItemToBuy.ID)) == true)
                    {
                        character.Coins = character.Coins - ItemToBuy.PriceHigh;
                        character.Update_Character();
                        character.bagManager.UpdateBag();

                        character.PlaySound("CashRegister", character.MapX, character.MapY);

                        Message message = new Message();
                        message.CreatureID = character.CreatureID;
                        message.Type = "server";
                        message.Text = "Server: You purchase " + ItemToBuy.Name;

                        character.AddMessage(message);

                        OpenVendorWindow(character);
                        return;
                    }
                }
            }

            if (ItemToBuy.Belt == true)
            {
                //Check if we have a slot before we run CreateItem
                if (character.beltManager.SlotsAvailable() > 0)
                {
                    //Attempt to add the new item to the belt.
                    if (character.beltManager.AddItem(Managers.GameHandler.CreateItem(character, ItemToBuy.ID)) == true)
                    {
                        character.Coins = character.Coins - ItemToBuy.PriceHigh;
                        character.Update_Character();
                        character.beltManager.UpdateBelt();

                        character.PlaySound("CashRegister", character.MapX, character.MapY);

                        Message message = new Message();
                        message.CreatureID = character.CreatureID;
                        message.Type = "server";
                        message.Text = "Server: You purchase " + ItemToBuy.Name;

                        character.AddMessage(message);

                        OpenVendorWindow(character);
                        return;
                    }
                }
            }

            if (character.LeftHand == null)
            {
                character.LeftHand = GameHandler.CreateItem(character, ItemToBuy.ID);
                character.Coins = character.Coins - ItemToBuy.PriceHigh;
                character.Update_Character();

                character.PlaySound("CashRegister", character.MapX, character.MapY);

                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Server: You purchase " + ItemToBuy.Name;

                character.AddMessage(message);

                OpenVendorWindow(character);

                return;
            }

            if (character.RightHand == null)
            {
                character.RightHand = GameHandler.CreateItem(character, ItemToBuy.ID);
                character.Coins = character.Coins - ItemToBuy.PriceHigh;
                character.Update_Character();

                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Server: You purchase " + ItemToBuy.Name;

                character.AddMessage(message);

                character.PlaySound("CashRegister", character.MapX, character.MapY);


                OpenVendorWindow(character);
                return;
            }

            Message msg = new Message();
            msg.CreatureID = character.CreatureID;
            msg.Type = "server";
            msg.Text = "No inventory slots available for " + ItemToBuy.Name;
            character.PlaySound("Ding", character.MapX, character.MapY);
            sendErrorNotification(character);
        }
        public void SellItem(Character character, long itemGUID)
        {
            if (this.MapID != character.MapID ||
                Math.Abs(this.MapX - character.MapX) > 2 || Math.Abs(this.MapY - character.MapY) > 2)
            {
                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Vendor is to far away";

                character.PlaySound("Ding", character.MapX, character.MapY);
                character.AddMessage(message);
                sendErrorNotification(character);
                return;
            }

            Loot ItemToSell = null;

            Loot leftHandItem = character.LeftHand;
            if (leftHandItem != null && ItemToSell == null)
            {
                if (leftHandItem.GameUID == itemGUID)
                {
                    ItemToSell = leftHandItem;
                    character.LeftHand = null;
                }
            }

            Loot rightHandItem = character.RightHand;
            if (rightHandItem != null && ItemToSell == null)
            {
                if (rightHandItem.GameUID == itemGUID)
                {
                    ItemToSell = rightHandItem;
                    character.RightHand = null;
                }
            }

            if (ItemToSell == null)
            {
                ItemToSell = character.bagManager.removeBagItem(itemGUID);
                if (ItemToSell != null)
                    character.bagManager.UpdateBag();
            }

            if (ItemToSell == null)
            {
                ItemToSell = character.beltManager.RemoveBeltItem(itemGUID);
                if (ItemToSell != null)
                    character.beltManager.UpdateBelt();
            }

            if (ItemToSell == null)
            {
                Message message = new Message();
                message.CreatureID = character.CreatureID;
                message.Type = "server";
                message.Text = "Cannot find item";

                character.PlaySound("Ding", character.MapX, character.MapY);
                character.AddMessage(message);
                sendErrorNotification(character);

                return;
            }

            ItemToSell = translateItemPrice(ItemToSell);

            character.Coins = character.Coins + ItemToSell.PriceLow;
            character.PlaySound("CashRegister", character.MapX, character.MapY);
            character.Update_Character();
            character.updateWeight();

            OpenVendorWindow(character);
        }
        private void sendErrorNotification(Character character)
        {
            try
            {
                character.ClientSocket.socket.BeginSend("<vendorerror />");
            }
            catch { }
        }
    }
}
